package com.partner.control;

import java.io.InputStream;
import java.util.List;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.google.gson.Gson;
import com.partner.domain.GroupUser;
import com.partner.domain.User;
import com.partner.service.GroupService;
import com.partner.util.IOUtil;

/**
 * 跟用户群有关的action
 * 
 * @author Jinshuan
 *
 */
@Controller
public class GroupAction {
	GroupService groupService = new GroupService();

	// 创建群
	@RequestMapping("/createGroup")
	public @ResponseBody boolean createGroup(InputStream is) {
		System.out.println("创建群组");
		String json = IOUtil.inputStreamToStream(is);
		System.out.println(json);
		if (json != null && !("".equals(json))) {
			String[] s = json.split(";");
			if (s.length == 5) {

				String groupName = s[0];
				String userid = s[1];
				String photo = s[2];
				String qunzhu = s[3];
				String nickname = s[4];
				return groupService.createGroup(groupName, userid, photo,
						qunzhu, nickname);
			}
		}
		return false;

	}

	// 获取群成员
	@RequestMapping("/queryGroupUserList")
	public @ResponseBody List<GroupUser> getGroupUser(InputStream is) {
		System.out.println("得到群用户");
		String json = IOUtil.inputStreamToStream(is);
		System.out.println(json);
		if (json != null && !("".equals(json))) {
			String[] s = json.split(";");
			return groupService.getGroupUser(s[0],s[1]);
		}
		
		return null;
	}
	
	//邀请好友进入群
	@RequestMapping("/yaoqingFriend")
	public @ResponseBody boolean yaoqingFriend(InputStream is){
		System.out.println("邀请好友加入群");
		String json = IOUtil.inputStreamToStream(is);
		System.out.println(json);
		if(json!=null&&!("".equals(json))){
			String[] s = json.split(",");
			if(s.length==3){
				String from = s[0];
				String groupId = s[2];
				String[] tos = s[1].split(";");
				System.out.println("tos--"+tos+"groupId--"+groupId);
				return groupService.yaoqingFriend(from, tos, groupId);
			}
		}
		
		return false;
	}
	
	//明确拒绝加入群
	@RequestMapping("/delineGroupYaoqing")
	public @ResponseBody boolean delineYaoqingTongZhi(InputStream is){
		System.out.println("明确拒绝加入群");
		String json = IOUtil.inputStreamToStream(is);
		if(json!=null&&!("".equals(json))){
			String[] s = json.split(";");
			if(s.length==3){
				String from_who = s[0];
				String to_who = s[1];
				String groupId = s[2];
				return groupService.delineYaoqingTongZhi(from_who, to_who, groupId);
			}
		}
		return false;
	}
	
	//同意加入群
	@RequestMapping("/argeeIntoGroup")
	public @ResponseBody boolean argeeIntoGroup(InputStream is){
		System.out.println("同意加入群");
		String json = IOUtil.inputStreamToStream(is);
		if(json!=null&&!("".equals(json))){
			String[] s = json.split(";");
			if(s.length==7){
				String userId = s[0];
				String groupId = s[1];
				String groupName = s[2];
				String photo = s[3];
				String qunzhu = s[4];
				String nickname = s[5];
				String from_who = s[6];
				return groupService.argeeIntoGroup(userId, groupId, groupName, photo, qunzhu, nickname, from_who);
			}
		}
		return false;
	}
}
